﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;
using System.Data;

namespace PilotMVC.Controllers
{
    [Authorize(Roles = "Admin")]
    public class QuestionController : Controller
    {
        private PilotEntities1 db = new PilotEntities1();

        public ActionResult Index()
        {
            using (var context = new PilotEntities1())
            {
                var model = new QuestionsView();              
                model.Questions = context.Questions.ToList(); 
                return View(model.Questions.ToList());
            }
        }
       [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string filterData)
        {
            using (var context = new PilotEntities1())
            {
                var model = new QuestionsView();
                switch (filterData)
                {
                    case "Pilot Active":
                        var active = context.Questions.Where(a => a.IsActive).ToList();
                        model.Questions = active.Where(q=>q.TestEngine.TestEngineDescription.Equals("JumpPilot")).ToList();
                        break;
                    case "Packer Active":
                        active = context.Questions.Where(a => a.IsActive).ToList();
                        model.Questions =active.Where(q => q.TestEngine.TestEngineDescription.Equals("Packer")).ToList();
                        break;
                    case "Pilot Inactive":
                        var pilots = context.Questions.Where(q => q.TestEngine.TestEngineDescription.Equals("JumpPilot")).ToList();
                        model.Questions = pilots.Where(q => q.IsActive==false).ToList();
                        break;
                    case "Packer Inactive":
                        var packers = context.Questions.Where(q => q.TestEngine.TestEngineDescription.Equals("Packer")).ToList();
                        model.Questions = packers.Where(q => q.IsActive==false).ToList();
                        break;
                    default:
                        model.Questions = context.Questions.ToList();
                        break;
                }
                return PartialView("_grid", model.Questions.ToList());
            }
        }



       public ActionResult Create()
        {
            ViewBag.Error= "Please select type!";
            ViewBag.TestEngineId = new SelectList(db.TestEngines.ToList(),"Id","TestEngineDescription");
            return View();
        }

        //
        // POST: /TestEngine/Create

       [HttpPost]
       public ActionResult Create(Question question, Option Optiontextcorrect1, Option Optiontextincorrect2, Option Optiontextincorrect3, Option Optiontextincorrect4)
       {


           if (ModelState.IsValid)
           {
               try
               {
                   if (question.TestEngineId == null)
                   {
                       throw new NullReferenceException();
                   }

                   question.TestEngineId = question.TestEngineId;
                   question.Id = Guid.NewGuid();


                   Optiontextcorrect1.Id = Guid.NewGuid();
                   Optiontextincorrect2.Id = Guid.NewGuid();
                   Optiontextincorrect3.Id = Guid.NewGuid();
                   Optiontextincorrect4.Id = Guid.NewGuid();


                   Optiontextcorrect1.QuestionId = question.Id;
                   Optiontextincorrect2.QuestionId = question.Id;
                   Optiontextincorrect3.QuestionId = question.Id;
                   Optiontextincorrect4.QuestionId = question.Id;

                   question.CreateDateTime = DateTime.Now;

                   db.Questions.AddObject(question);
                   db.Options.AddObject(Optiontextcorrect1);
                   db.Options.AddObject(Optiontextincorrect2);
                   db.Options.AddObject(Optiontextincorrect3);
                   db.Options.AddObject(Optiontextincorrect4);

                   db.SaveChanges();
                   return RedirectToAction("Index");
               }
               catch (NullReferenceException)
               {
                   ModelState.AddModelError("", "Select id");
               }
           }
           else
           {
               ModelState.AddModelError("", "Select Question Type");
           }
           ViewBag.TestEngineId = new SelectList(db.TestEngines.ToList(), "Id", "TestEngineDescription");
           return View(question);
       }

        public ActionResult ViewOptions(Guid a)
        {
            List<Option> question = new List<Option>();
            using (var context = new PilotEntities1())
            {
                question = context.Options.Where(o => o.QuestionId.Equals(a)).ToList();
            }

            return View(question);
        }

        //public ActionResult ViewOptions(Guid a)
        //{
        //    List<Option> question = new List<Option>();
        //    using (var context = new PilotEntities1())
        //    {
        //        question = context.Options.Where(o => o.QuestionId.Equals(a)).ToList();
        //    }

        //    return View(question);
        //}

        // GET: /Question/Edit/5

        public ActionResult Edit(Guid id)
        {
            Question question = db.Questions.Include("Options").Single(q => q.Id == id);
            ViewBag.TestEngineId = new SelectList(db.TestEngines, "Id", "Id", question.TestEngineId);
            question.TestEngine = question.TestEngine;
            return View(question);
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                var theOriginalQuestion = db.Questions.Where(q => q.Id.Equals(question.Id)).FirstOrDefault();

                var theEngine = db.TestEngines.Where(a => a.Id.Equals(question.TestEngineId)).FirstOrDefault();

                var activeQsCount = db.Questions.Where(a => a.TestEngine.TestEngineDescription.Equals(theEngine.TestEngineDescription)).Where(a => a.IsActive).Count();

                var highNumQsToken = db.TestTokens.Where(a => a.TestEngine.TestEngineDescription.Equals(theEngine.TestEngineDescription)).ToList();

                var high = highNumQsToken.OrderByDescending(t => t.NoOfQuestions).FirstOrDefault();

                if (theOriginalQuestion.IsActive == true && question.IsActive == false)
                {
                    if (high.NoOfQuestions == activeQsCount)
                    {
                        ModelState.AddModelError("Error Disabling Question : ", "You Cannot Disable Questions At This Time");
                        return View("Edit", question);
                    }

                }



                theOriginalQuestion.IsActive = question.IsActive;

                //db.Questions.Attach(question);
                //db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TestEngineId = new SelectList(db.TestEngines, "Id", "Id", question.TestEngineId);

            return View(question);
        }

        //Details
        public ViewResult Details(Guid id)
        {
            using (var context = new PilotEntities1())
            {
                Question question = context.Questions.Include("Options").Single(q => q.Id == id);
                return View(question);
            }
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
